package com.v9.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.v9.common.dto.IdDto;
import com.v9.common.dto.PageInfoDto;
import com.v9.common.exception.BizException;
import com.v9.common.web.AbstractController;
import com.v9.system.domain.Application;
import com.v9.system.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@RestController
@Tag(name = "应用管理")
@Slf4j
@RequestMapping(value = "/sys/app", method = RequestMethod.POST)
public class AppplicationController extends AbstractController {

    @Resource
    private ApplicationService applicationService;


    @RequestMapping("/add")
    @Operation(summary = "添加应用")
    public void add(@RequestBody @Valid ApplicationDto dto) {
        Application query = new Application();
        query.setCode(dto.getCode());
        Long count = applicationService.selectCount(query);
        if (null != count && count > 0) {
            throw new BizException( "应用编码重复");
        }
        Application application = clone(Application.class, dto);
        applicationService.insert(application);
    }

    @RequestMapping("/update")
    @Operation(summary = "修改应用信息")
    public void update(@RequestBody @Valid ApplicationUpdateDto applicationUpdateDto) {
        Application application = clone(Application.class, applicationUpdateDto);
        applicationService.updateById(application);
    }


    @RequestMapping("/list")
    @Operation(summary = "应用列表")
    public Page<Application> list(@RequestBody ApplicationQueryDto dto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name", dto.getName());
        queryWrapper.like("code", dto.getCode());
        return applicationService.queryByPage(queryWrapper, dto.getPageable());
    }

    @RequestMapping("/delete")
    @Operation(summary = "删除")
    public void delete(@RequestBody IdDto idDto) {
        applicationService.remove(idDto.getId());
    }


    @Data
    public static class ApplicationDto {

        @NotBlank(message = "应用名称不能为空")
        @Schema(description = "应用名称")
        private String name;

        @Schema(description = "应用描述")
        private String description;

        @NotBlank(message = "应用编码不能为空")
        @Schema(description = "应用编码")
        private String code;
    }


    @Data
    public static class ApplicationUpdateDto {

        @NotNull(message = "应用ID不能为空")
        @Schema(description = "应用ID")
        private Long id;

        @Schema(description = "应用名称")
        private String name;

        @Schema(description = "应用描述")
        private String description;

        @Schema(description = "应用编码")
        private String code;
    }


    @Data
    public static class ApplicationQueryDto extends PageInfoDto {

        @Schema(description = "应用名称")
        private String name;

        @Schema(description = "应用编码")
        private String code;
    }
}
